package com.example.jeong.yuejian10.Model;


import com.example.jeong.yuejian10.Bean.AttentionBean;
import com.example.jeong.yuejian10.Bean.EnterTrainBean;
import com.example.jeong.yuejian10.Bean.IsAttentionBean;
import com.example.jeong.yuejian10.Bean.TrainBean;
import com.example.jeong.yuejian10.Listener.RetrofitListener;
import com.example.jeong.yuejian10.Service.TrainService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.jeong.yuejian10.constant.Constant.BASE_URL;


public class TrainModel{
    private Retrofit retrofit;
    public TrainModel(){
        //创建实例
        retrofit =new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //培训列表
    public void getTrainlist(final RetrofitListener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.getTrainlist();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBeen = response.body();
                listener.onSuccess(trainBeen);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }

    public void getTrainlista(final RetrofitListener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.getTrainlista();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBeen = response.body();
                listener.onSuccess(trainBeen);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }

    public void getTrainlistb(final RetrofitListener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.getTrainlistb();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBeen = response.body();
                listener.onSuccess(trainBeen);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }
    public void getTrainlistc(final RetrofitListener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.getTrainlistc();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBeen = response.body();
                listener.onSuccess(trainBeen);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }
    public void getTrainlistd(final RetrofitListener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.getTrainlistd();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBeen = response.body();
                listener.onSuccess(trainBeen);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }


    //培训详情
    public Call<TrainBean> getDetail(int t_id){
        TrainService trainService = retrofit.create(TrainService.class);
        return trainService.getDetail(t_id);
    }

    //搜索培训
    public Call<List<TrainBean>> searchTrain(String t_name){
        TrainService trainService = retrofit.create(TrainService.class);
        return trainService.searchTrain(t_name);
    }



    //报名培训
    public Call<EnterTrainBean> addtrainn(int u_id, int t_id){
        TrainService trainService = retrofit.create(TrainService.class);
        return trainService.addtrainn(u_id,t_id);
    }

    //判断是否关注
    public Call<IsAttentionBean> getIsCollected(int u_id, int select_id, String flag){
        TrainService trainService=retrofit.create(TrainService.class);
        return trainService.getIsCollected(u_id, select_id, flag);
    }

    //用户关注培训接口
    public Call<AttentionBean> userCollectTrain(int u_id, int t_id){
        TrainService trainService=retrofit.create(TrainService.class);
        return trainService.userCollectTrain(u_id,t_id);
    }

}